//
//  LeKeyFobService.h
//
//
//  Created by linktoplinktop on 2019/2/18.
//  Copyright © 2019 Linktop. All rights reserved.
//  连接的蓝牙设备操作

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <UIKit/UIKit.h>
#import "SROTADefines.h"
#import "SRDeviceInfo.h"
#import "SRHistoryData.h"
#import "SRHistoryDataSR03.h"

#import "LTSRingSDK.h"
#import "SDKConstant.h"
#import "SrEcgData.h"
#import "SrEcgAlgorithmResults.h"
/****************************************************************************/
/*						Service Characteristics								*/
/****************************************************************************/
//extern NSString *kSetDeviceAlertServiceUUIDString;
//extern NSString *kSetMobileAlertServiceUUIDString;
//extern NSString *kBatteryServiceUUIDString;
//extern NSString *serviceUUIDString;
//extern NSString *kSetDeviceAlertCharacteristicUUIDString;
//extern NSString *kSetMobileAlertCharacteristicUUIDString;
//extern NSString *kBatteryCharacteristicUUIDString;

/****************************************************************************/
/*								Protocol									*/
/****************************************************************************/
@class SRBLeService;
@class SRPacket;

#define BLES_WEAK_SELF  __weak typeof(self) weakSelf = self;
#define BLES_STRONG_SELF  __strong typeof(weakSelf) strongSelf = weakSelf; \
                     if (!strongSelf) return; \

extern NSString * const BluetoothManagerReceiveDevices;
extern NSString * const BluetoothManagerConnectingToDevice;
extern NSString * const BluetoothManagerConnectedToDevice;
extern NSString * const BluetoothManagerDisconnectedFromDevice;
extern NSString * const BluetoothManagerConnectionFailed;

extern NSString * const GenericServiceManagerDidReceiveValue;
extern NSString * const GenericServiceManagerDidSendValue;
extern NSString * const GenericServiceManagerReadError;
extern NSString * const GenericServiceManagerWriteError;


extern NSString * const SERVICE_UUID_DATA; // 扫描过滤的uuid

//extern NSString * const serviceUUIDString;

@protocol StLeKeyFobProtocol<NSObject>
@required
// 电池服务初始化完毕
- (void)stkeyFobServiceBatteryReady;

- (void) stkeyFobServiceDidChangeStatus:(SRBLeService*)service;
- (void) stkeyFobServiceDidConnectPeripheral:(SRBLeService*)service;
- (void) stkeyFobServiceDidDisconnectPeripheral:(SRBLeService*)service;
- (void) stkeyFobServiceDidReadRSSI:(SRBLeService*)service;
- (void) stkeyFobServiceDidReset;




/// 实时hrv
/// @param hrv hrv
- (void)stkeyFobServiceRealtimeHRV:(NSNumber *)hrv BloodOxygen:(NSNumber *)bloodOxygen HeartRate:(NSNumber *)hr Status:(REALTIME_STATUS)status;


/// 实时计步
/// @param steps 计步
- (void)stkeyFobServiceRealtimeSteps:(NSNumber *)steps  DEPRECATED_MSG_ATTRIBUTE("协议1.9开始废弃");


/// 实时体温
/// @param temperature 体温
- (void)stkeyFobServiceRealtimeTemperaure:(NSNumber *)temperature;


// 回调设备信息
- (void)stkeyFobServiceDeviceInfo:(SRDeviceInfo *)devInfo;


/// 读写数据主特征准备完毕
- (void) stkeyFobServiceDidReadyForReadWrite:(SRBLeService *)service;


/// 电量回调
/// @param batteryLevel 电量
/// @param isCharging 是否充电
- (void) stkeyFobServiceBatteryLevel:(NSUInteger)batteryLevel IsCharging:(BOOL)isCharging;

- (void) stkeyFobServiceSN:(NSString *)sn OriginSNData:(NSData *)sndata;

- (void) stkeyFobServiceSampleRate:(uint8_t)smpRate WaveSwitch:(BOOL)isOn;

- (void) stkeyFobServiceHistoryTimeout;
- (void) stkeyFobServiceHistoryCount:(NSInteger)count;
- (void) stkeyFobServiceHistoryData:(SRHistoryData *)hisData IsComplete:(BOOL)isComplete;

// sr03设备的历史回调
- (void) stkeyFobServiceHistorySR03Data:(SRHistoryDataSR03 *)sr03HisData IsComplete:(BOOL)isComplete;



///
/// @param cmd app下发的命令字 废弃 V1.0.7 2023-8-8
/// @param isSuccess 是否执行成功
//- (void) stkeyFobServiceSendCmdExcute:(uint8_t)cmd Succ:(BOOL)isSuccess DEPRECATED_MSG_ATTRIBUTE("deprecate from V1.0.7, use stkeyFobServiceSendCmdExcute:Succ:Reason: instead");


/// 从1.0.7开始使用
/// @param cmd app下发的命令字
/// @param isSuccess 是否执行成功
/// @param reason 失败原因
- (void) stkeyFobServiceSendCmdExcute:(uint8_t)cmd Succ:(BOOL)isSuccess Reason:(CMD_EXECTE_ERROR_REASON)reason;


///
/// @param isSosOpen
/// @param doubleTapTimes
/// @param singleTapInterval
/// @param tapDetectionThreshold
/// @param doubleTapEffectStartTime
/// @param doubleTapEffectEndTime
/// @param isBinded
- (void) stkeyFobService:(BOOL)isSosOpen
          DoubleTapTimes:(uint8_t)doubleTapTimes
       SingleTapInterval:(uint8_t)singleTapInterval TapDetectionThreshold:(uint8_t)tapDetectionThreshold
DoubleTapEffectStartTime:(uint8_t)doubleTapEffectStartTime DoubleTapEffectEndTime:(uint8_t)doubleTapEffectEndTime Isbinded:(BOOL)isBinded;

- (void) stkeyFobServiceSOShappen;
- (void)stkeyFobServiceSOSData:(float)temperature HeartRate:(NSInteger)hr BloodOxygen:(NSInteger)bloodOxygen;

/// 设备上报R1密文
/// - Parameter r1: 密文
- (void)stkeyFobServiceAESRecvR1:(NSData *)r1;

/// 设备上报认证最后结果
/// - Parameter isAuthed: YES = auth pass
- (void)stkeyFobServiceAESAuthResult:(BOOL)isAuthed;

// 回调ecg波形, ecgObj.isAlgorithData = NO为原始波形
- (void)stkeyFobServiceEcg:(SrEcgData *)ecgObj;

- (void)stkeyFobServiceEcgAndPPg:(SrEcgData *)ecgPpgObj;

// 回调ecg算法结果
- (void)stkeyFobServiceEcgResult:(SrEcgAlgorithmResults *)ecgResult;

- (void)stkeyFobServiceEcgLeadOn:(BOOL)leadOn;

/*   AES */

// 设置
- (void)stkeyFobServiceHrMeasuDuration:(NSInteger)durations PD_THRESHOLD:(uint32_t)pd_threshold PD_RANGE_1:(uint32_t)pd_range_1 PD_RANGE_2:(uint32_t)pd_range_2;

// 波形上报
- (void)stkeyFobServiceIrData:(uint16_t)irData; // 血氧红外
- (void)stkeyFobServiceRedData:(uint16_t)redData; // 红光
- (void)stkeyFobServiceOxygenMeasureInterval:(uint16_t)minuteInterval OxygenSwitch:(BOOL)on;

@end

// ota 协议
@protocol StLeKeyOTAProtocol<NSObject>
@required
// ota 的数据返回
-(void)srOtaDidReceiveValue:(CBCharacteristic *)characteristic;


@end

/****************************************************************************/
/*						Temperature Alarm service.                          */
/****************************************************************************/
@interface SRBLeService : NSObject


/* Querying Sensor */
@property (readonly) CBPeripheral *peripheral;
@property (assign, readonly) CGFloat battery;
@property (assign, nonatomic) BOOL isAlerting;
@property (strong, nonatomic)NSNumber *rssi; // 信号强度 负数
@property (assign, nonatomic) BOOL isManualDisconnect;      //标记用户手动断开
@property (assign, nonatomic) BOOL isExceptional;           //异常断开
@property (assign, nonatomic) NSInteger reconnectCounts;    //重连次数
@property (strong, nonatomic)NSString *advDataLocalName;    //外设本地名称
@property (strong, nonatomic)NSString *macAddress;

@property(assign, nonatomic, readonly) int suotaVersion;
@property(assign, nonatomic, readonly) int suotaMtu;
@property(assign, nonatomic, readonly) int suotaPatchDataSize;
@property(assign, nonatomic, readonly) int suotaL2CapPsm;

@property(strong, nonatomic) SRPacket *packetObj;  //封包 解包

@property(assign, nonatomic)DEV_COLOR deviceColor; // 设备颜色
@property(assign, nonatomic)int deviceSize;  // 设备尺寸

@property(assign, nonatomic)MAIN_CHIP_TYPE mainChipType; // 主芯片型号
@property(assign, nonatomic)NSUInteger deviceGeneration;  // 迭代版本

@property(strong, nonatomic)NSNumber *needOemAuth; //是否需要oem厂家认证
@property(strong, nonatomic)NSString *snString;
@property(strong, nonatomic)NSData *authSnData; // 认证使用sn data
@property(strong, nonatomic)NSString * softWareVersion;

@property(assign, nonatomic) BOOL hasBeenAuth; // 标记已认证过,无论是否通过认证都是YES

@property(strong, nonatomic)NSNumber *isWirenessCharge; // 是否无线充

@property(assign, nonatomic)BOOL isBinded; // 是否已标记绑定

@property(assign, nonatomic)NSInteger hrMeasureDurations;
@property(assign, nonatomic)BOOL isSupportSportMode;

@property(assign, nonatomic)NSUInteger sampleRate; // 采样率 不开放
@property(assign, nonatomic)BOOL isWaveDataOn;  // 实时心率血氧测量波形是否上报 开放
@property(strong, nonatomic)NSNumber *chargeType; // 充电模式
@property(strong, nonatomic)NSNumber *scanIsCharge, *scanBatteryLevel;

@property(assign, nonatomic)BOOL isSupportEcg;  // 是否支持ecg
@property(assign, nonatomic)NSUInteger projectId;



- (id) initWithPeripheral:(CBPeripheral *)peripheral controller:(id<StLeKeyFobProtocol>)delegate;
- (instancetype) initWithPeripheral:(CBPeripheral *)peripheral;
- (void) reset;
- (void) start;
- (void) stopRssiTimer;

-(void)setPerpheralDelegate:(id<StLeKeyFobProtocol>)delegate;
-(id<StLeKeyFobProtocol>)perpheralDelegate;
-(void)setAdvData:(NSDictionary *)advertisementData;
/* sdk 不开放方法 */


/// 获取心率血氧开关
/// @pram both 都开, NO = 单开心率
/// @param isStart isStart = NO 关闭  isStart = yes 开启
-(void)deviceOxygenAndHrvBoth:(BOOL)both IsOpen:(BOOL)isStart;

-(void)cmdDeviceShutDown;
-(void)cmdDeviceReboot;
-(void)cmdDeviceBattery;
-(void)cmdSn;
-(void)cmdHistoryCount; // 获取历史个数 流程开始
-(void)cmdHistoryData; // 获取历史

-(void)cmdClearHistory;
-(void)cmdDevInfo;
-(void)cmdDevRealMeasureWaveOn:(BOOL)isOn;

-(void)cmdTimeSync; // 时间同步
-(void)cmdGetSteps;
-(void)cmdGetTemperature;
-(void)cmdFactoryReset;
-(void)cmdBindeDevice:(BOOL)bind; //绑定解绑

-(void)cmdGetDevInfoFive;
-(void)cmdSetMeasureDuration:(NSInteger)durations;

///
/// @param start 开关
/// @param valInterval 设备取值间隔 秒
/// @param durationMIn 设备持续时间 分钟
-(void)cmdSportMode:(BOOL)start ValueINterval:(uint16_t)valInterval Duration:(uint16_t)durationMIn WorkType:(WORKOUT_TYPE)workoutType;

// 开始oem认证
-(void)cmdStartOemAuth;

// 发送解密后的明文
-(void)cmdOEMSendR2:(NSData *)r2;

-(BOOL)characteristicReadWriteReady;

/// 血氧测量设置
/// @param startHor 开始 时
/// @param startMin 开始 时分
/// @param startSecond 开始 秒
/// @param endHor 结束 时
/// @param endMin 结束 分
/// @param endSec 结束 秒
/// @param interval 间隔 单位:分钟
-(BOOL)cmdSettingOxygenMeasure:(NSUInteger)startHor StartMin:(NSUInteger)startMin StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec Interval:(NSUInteger)interval IsOn:(BOOL)isOn;

-(void)cmdSettingHeartRateMeasurInterval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval IsOn:(BOOL)isOn;

-(void)cmdSettingSportMode:(NSUInteger)startHor StartMin:(NSUInteger)startMin
                  StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor
                    EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec
                HrIntreval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval
                  BloodOxygenInterval:(NSUInteger)oxygeninterval
                      IsOn:(BOOL)isOn;

-(void)cmdSOSSettingDoubleTapTimes:(uint8_t)doubleTapTimes
    SingleTapInterval:(uint8_t)singleTapInterval TapDetectionThreshold:(uint8_t)tapDetectionThreshold
          DoubleTapEffectStartTime:(uint8_t)doubleTapEffectStartTime DoubleTapEffectEndTime:(uint8_t)doubleTapEffectEndTime Open:(BOOL)sosOpen;

-(void)cmdECGMeasure:(BOOL)on SampleRate:(uint16_t)sampleRate InnerClockSource:(uint8_t)clockSource EcgPgaGain:(uint8_t)ecgPgaGain DispSrc:(uint8_t)dispSrc;

-(void)cmdEcgAndPpgMeasure:(BOOL)on;

- (void) otaWriteValueWithoutResponse:(CBUUID*)serviceUUID characteristicUUID:(CBUUID*)characteristicUUID data:(NSData*)data;

- (void) otaWriteValue:(CBUUID*)serviceUUID characteristicUUID:(CBUUID*)characteristicUUID data:(NSData*)data;

- (void) setNotifications:(CBUUID*)serviceUUID characteristicUUID:(CBUUID*)characteristicUUID enable:(BOOL)enable;



/* sdk 不开放方法 */

+ (CBUUID*) SPOTA_SERVICE_CBUUID;

+ (CBUUID*) SPOTA_MEM_DEV_CBUUID ;

+ (CBUUID*) SPOTA_GPIO_MAP_CBUUID ;

+ (CBUUID*) SPOTA_MEM_INFO_CBUUID ;

+ (CBUUID*) SPOTA_PATCH_LEN_CBUUID ;

+ (CBUUID*) SPOTA_PATCH_DATA_CBUUID ;

+ (CBUUID*) SPOTA_SERV_STATUS_CBUUID ;

+ (CBUUID*) SUOTA_VERSION_CBUUID ;

+ (CBUUID*) SUOTA_PATCH_DATA_CHAR_SIZE_CBUUID ;

+ (CBUUID*) SUOTA_MTU_CBUUID ;

+ (CBUUID*) SUOTA_L2CAP_PSM_CBUUID ;



@end
